<template>
  <page-header-wrapper :title="false">
    <a-card title="商标库">
      <template slot="extra">
        <a-button size="small" type="primary" icon="plus" @click="$router.push({ name: 'AddForm' })">添加商标</a-button>
        <a-button size="small" type="danger" style="margin-left: 5px" @click="onBatch">批量上传</a-button>
      </template>
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="商标名称">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="请输入商标名称" :allowClear="true" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="商标类别">
                <a-select v-model="queryParam['filter[category]']" placeholder="请选择商标类别" :allowClear="true">
                  <a-select-option v-for="(item,index) in categoryOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="商标类型">
                  <a-select v-model="queryParam['filter[tm_type]']" placeholder="请选择商标类型" :allowClear="true">
                    <a-select-option v-for="(item,index) in typeOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="公司自持">
                  <a-select disabled v-model="queryParam['filter[business_name]']" placeholder="请选择是否为公司自持">
                    <a-select-option :value="1">是</a-select-option>
                    <a-select-option :value="2">否</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="主体类型">
                  <a-select v-model="queryParam['filter[tm_applicant_type]']" placeholder="请选择申请主体类型">
                    <a-select-option v-for="(item,index) in applyOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="售价区间">
                  <div style="display: flex;align-items: center">
                    <a-input v-model="queryParam['filter[price_start]']" placeholder="请输入期望售价"/>&nbsp;&nbsp;~&nbsp;&nbsp;<a-input v-model="queryParam['filter[price_end]']" placeholder="请输入期望售价"/>
                  </div>
                </a-form-model-item>
              </a-col>
<!--              <a-col :md="6" :sm="24">-->
<!--                <a-form-item label="官费区间">-->
<!--                  <div style="display: flex;align-items: center">-->
<!--                    <a-input v-model="queryParam['filter[official_fee_start]']" placeholder="请输入期望官费"/>&nbsp;&nbsp;~&nbsp;&nbsp;<a-input v-model="queryParam['filter[official_fee_end]']" placeholder="请输入期望官费"/>-->
<!--                  </div>-->
<!--                </a-form-item>-->
<!--              </a-col>-->
              <a-col :md="6" :sm="24">
                <a-form-item label="成本区间">
                  <div style="display: flex;align-items: center">
                    <a-input v-model="queryParam['filter[cost_fee_start]']" placeholder="请输入期望成本"/>&nbsp;&nbsp;~&nbsp;&nbsp;<a-input v-model="queryParam['filter[cost_fee_end]']" placeholder="请输入期望成本"/>
                  </div>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="注册号">
                  <a-input v-model="queryParam['filter[tm_reg_no]']" placeholder="请输入商注册号" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="下证时间">
                  <a-range-picker v-model="queryParam['end_time_start']" @change="onChange" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          size="middle"
          ref="table"
          :row-key="(record) => record.id"
          :columns="wareroomColumns"
          :data="wareroomData"
        >
          <template slot="title">
            <div style="color: #ff0000;font-weight: bold">商标总数：{{count}}</div>
          </template>
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterCategory"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterType"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in typeOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterCompany"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in isCompanyOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterStatus"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue"
              @change="onChangeDate"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset(clearFilters)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange1"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue1"
              @change="onChangeDate1"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset1(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="is_company" slot-scope="text">
            {{isCompanyOpts[text]}}
          </template>
          <template slot="tm_type" slot-scope="text">
            <div v-for="(item, index) in typeOpts" :key="index">
              <div v-if="text == index"><a-tag color="red">{{item}}</a-tag></div>
            </div>
          </template>
          <template slot="category" slot-scope="text">
            <a-tag color="red">{{categoryOpts[text]}}</a-tag>
          </template>
          <template slot="tm_status" slot-scope="text">
            <a-tag color="red">{{statusOpts[text]}}</a-tag>
          </template>
<!--          <template slot="action">-->
<!--            <a-button size="small" type="primary">修改</a-button>-->
<!--            <a-button size="small" type="danger">删除</a-button>-->
<!--          </template>-->
          <template slot="action" slot-scope="text, record">
            <a-button size="small" type="primary" @click="$router.push({ name: 'WareDetail', params: { id: record.id } })">查看</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      title="批量上传商标"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-alert>
        <template slot="message">
          点击下载批量上传CSV模版<a href="https://design-upload.oss-cn-beijing.aliyuncs.com/import.csv">下载</a>
        </template>
      </a-alert>
      <a-form-model
        ref="uploadForm"
        :model="uploadForm"
      >
        <a-form-model-item label="上传CSV文件">
          <a-upload
            name="file"
            accept=".csv"
            :multiple="true"
            :headers="headers"
            :action="uploadUrl"
            @change="onChange1"
          >
            <a-button size="small" icon="upload" type="primary">选择文件</a-button>
          </a-upload>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import transactionColumns from '@/columns/transaction'
  import { batchImport, getTradeList } from '@/api/tm.shop'
  import apis from '@/api'
  import storage from 'store'
  import { ACCESS_TOKEN } from '@/store/mutation-types'

  export default {
    name: 'Wareroom',
    components: { STable, Ellipsis },
    data () {
      return {
        visible: false,
        queryParam: {},
        advanced: true,
        wareroomColumns: transactionColumns.wareroom,
        wareroomData: parameters => {
          return getTradeList(Object.assign(parameters, this.queryParam)).then(res => {
            this.categoryOpts = res.result.category_opts
            this.regionOpts = res.result.tm_region_opts
            this.labelOpts = res.result.tm_label_opts
            this.statusOpts = res.result.tm_status_opts
            this.typeOpts = res.result.tm_type_opts
            this.applyOpts = res.result.tm_applicant_opts
            this.isCompanyOpts = res.result.isCompanyOpts
            this.groupOpts = res.result.group_type
            this.count = res.result.total_count
            return res.result
          })
        },
        regionOpts: {},
        statusOpts: {},
        labelOpts: {},
        typeOpts: {},
        applyOpts: {},
        groupOpts: {},
        categoryOpts: {},
        isCompanyOpts: {},
        uploadUrl: apis.UploadFile,
        headers: {
          'Access-Token': storage.get(ACCESS_TOKEN)
        },
        uploadForm: {},
        createValue: [],
        createValue1: [],
        count: ''
      }
    },
    methods: {
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onChange (date, dateString) {
        this.queryParam['filter[end_time_start]'] = dateString[0]
        this.queryParam['filter[end_time_end]'] = dateString[1]
      },
      onBatch () {
        this.uploadForm = {}
        this.visible = true
      },
      onChange1 (info) {
        if (info.file.status === 'uploading') {
          return false
        }
        if (info.file.status === 'done') {
          this.uploadForm.filename = info.file.name
          this.uploadForm.file = info.file.response.result.url
        }
      },
      onSubmit () {
        const app = this
        const data = {}
        data['file[value]'] = this.uploadForm.file
        batchImport(data).then(res => {
          if (res.status === 0) {
            app.visible = false
            app.$refs.table.refresh(true)
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      handleSelect (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      },
      onChangeDate (date, dateString) {
        this.createValue = date
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      onChangeDate1 (date, dateString) {
        this.createValue1 = date
        this.queryParam['filter[end_time_start]'] = dateString[0]
        this.queryParam['filter[end_time_end]'] = dateString[1]
      },
      handleRange (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.$refs.table.refresh(true)
      },
      rangeReset (clearFilters) {
        clearFilters()
        this.createValue = []
        delete this.queryParam['filter[created_at_start]']
        delete this.queryParam['filter[created_at_end]']
        this.$refs.table.refresh(true)
      },
      rangeReset1 (clearFilters) {
        clearFilters()
        this.createValue1 = []
        delete this.queryParam['filter[end_time_start]']
        delete this.queryParam['filter[end_time_end]']
        this.$refs.table.refresh(true)
      }
    }
  }
</script>

<style scoped>

</style>
